
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Instrumenting Jersey 2.x | Metrics</title>
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css"/>
    <link rel="stylesheet" href="../_static/metrics.css" type="text/css"/>
    <link rel="top" title="Metrics" href="../index.html"/>
    <link rel="up" title="User Manual" href="index.html"/>
    <link rel="next" title="Instrumenting Jetty" href="jetty.html"/>
    <link rel="prev" title="Instrumenting JDBI" href="jdbi.html"/>
    <style lang="text/css">
        #top-bar, #top-bar small, #top-bar a {
            text-shadow: 0px -1px 0px #cf2c0f;
            color: #fff;
        }
        
        #top-bar {
            background-color: #ED4A2D;
            background-image: -moz-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -ms-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ff684b), to(#cf2c0f));
            background-image: -webkit-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -o-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: linear-gradient(top, #ff684b, #cf2c0f);
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#ff684b', endColorstr = '#cf2c0f', GradientType = 0);
            -webkit-border-radius: 0;
            -moz-border-radius: 0;
            border-radius: 0;
        }


        .hero-unit {
            background-image: url("../_static/metrics-hat.png") !important;
            background-repeat: no-repeat !important;
            background-position: 30px 50px;
        }

        .hero-unit div.section {
            padding-left: 200px !important;
        }
    </style>
</head>
<body>
    <a href="https://github.com/dropwizard/metrics">
        <img style="position: absolute; top: 0; right: 0; border: 0;"
             src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"
             alt="Fork me on GitHub"></a>
    <div class="navbar">
        <div class="navbar-inner container-fluid" id="top-bar">
            <header class="row-fluid">
                <h1 class="span12" id="title"><img class="logo" src="../_static/metrics-logo.png" alt="Logo"/>
                    <a href="../index.html">Metrics</a>
                    <small>Mind the gap.</small>
                </h1>
            </header>
        </div>
    </div>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span3" id="sidebar">
                <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../getting-started.html">Getting Started</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="core.html">Metrics Core</a></li>
<li class="toctree-l2"><a class="reference internal" href="healthchecks.html">Health Checks</a></li>
<li class="toctree-l2"><a class="reference internal" href="ehcache.html">Instrumenting Ehcache</a></li>
<li class="toctree-l2"><a class="reference internal" href="collectd.html">Reporting to Collectd</a></li>
<li class="toctree-l2"><a class="reference internal" href="graphite.html">Reporting to Graphite</a></li>
<li class="toctree-l2"><a class="reference internal" href="httpclient.html">Instrumenting Apache HttpClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="jdbi.html">Instrumenting JDBI</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="">Instrumenting Jersey 2.x</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#supported-annotations">Supported Annotations</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="jetty.html">Instrumenting Jetty</a></li>
<li class="toctree-l2"><a class="reference internal" href="log4j.html">Instrumenting Log4j</a></li>
<li class="toctree-l2"><a class="reference internal" href="logback.html">Instrumenting Logback</a></li>
<li class="toctree-l2"><a class="reference internal" href="jvm.html">JVM Instrumentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="json.html">JSON Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="servlets.html">Metrics Servlets</a></li>
<li class="toctree-l2"><a class="reference internal" href="servlet.html">Instrumenting Web Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="third-party.html">Third Party Libraries</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../about/index.html">About Metrics</a></li>
</ul>

                <hr/>
                <ul>
                    <li><a href="https://www.javadoc.io/doc/io.dropwizard.metrics/metrics-core/4.1.2/">Javadoc</a></li>
                    <li><a href="https://groups.google.com/forum/#!forum/metrics-user">Mailing List</a></li>
                </ul>
            </div>
            <div class="span9" id="body">
                
  <div class="section" id="instrumenting-jersey-2-x">
<span id="manual-jersey"></span><h1>Instrumenting Jersey 2.x</h1>
<p>Jersey 2.x changed the API for how resource method monitoring works, so a new
module <code class="docutils literal"><span class="pre">metrics-jersey2</span></code> provides <code class="docutils literal"><span class="pre">InstrumentedResourceMethodApplicationListener</span></code>,
which allows you to instrument methods on your <a class="reference external" href="https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/index.html">Jersey 2.x</a> resource classes:</p>
<p>The <code class="docutils literal"><span class="pre">metrics-jersey2</span></code> module provides <code class="docutils literal"><span class="pre">InstrumentedResourceMethodApplicationListener</span></code>, which allows
you to instrument methods on your <a class="reference external" href="https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/index.html">Jersey 2.x</a> resource classes:</p>
<p>An instance of <code class="docutils literal"><span class="pre">InstrumentedResourceMethodApplicationListener</span></code> must be registered with your Jersey
application&#8217;s <code class="docutils literal"><span class="pre">ResourceConfig</span></code> as a singleton provider for this to work.</p>
<div class="highlight-java"><div class="highlight"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ExampleApplication</span> <span class="kd">extends</span> <span class="n">ResourceConfig</span> <span class="o">{</span>
    <span class="o">.</span>
    <span class="o">.</span>
    <span class="o">.</span>
    <span class="n">register</span><span class="o">(</span><span class="k">new</span> <span class="n">InstrumentedResourceMethodApplicationListener</span> <span class="o">(</span><span class="k">new</span> <span class="n">MetricRegistry</span><span class="o">()));</span>
    <span class="n">config</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="na">register</span><span class="o">(</span><span class="n">ExampleResource</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
    <span class="o">.</span>
    <span class="o">.</span>
    <span class="o">.</span>
<span class="o">}</span>

<span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/example&quot;</span><span class="o">)</span>
<span class="nd">@Produces</span><span class="o">(</span><span class="n">MediaType</span><span class="o">.</span><span class="na">TEXT_PLAIN</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ExampleResource</span> <span class="o">{</span>
    <span class="nd">@GET</span>
    <span class="nd">@Timed</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">show</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="s">&quot;yay&quot;</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@GET</span>
    <span class="nd">@Metered</span><span class="o">(</span><span class="n">name</span> <span class="o">=</span> <span class="s">&quot;fancyName&quot;</span><span class="o">)</span>
    <span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/metered&quot;</span><span class="o">)</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">metered</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="s">&quot;woo&quot;</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@GET</span>
    <span class="nd">@ExceptionMetered</span><span class="o">(</span><span class="n">cause</span> <span class="o">=</span> <span class="n">IOException</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
    <span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/exception-metered&quot;</span><span class="o">)</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">exceptionMetered</span><span class="o">(</span><span class="nd">@QueryParam</span><span class="o">(</span><span class="s">&quot;splode&quot;</span><span class="o">)</span> <span class="nd">@DefaultValue</span><span class="o">(</span><span class="s">&quot;false&quot;</span><span class="o">)</span> <span class="kt">boolean</span> <span class="n">splode</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">splode</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">throw</span> <span class="k">new</span> <span class="n">IOException</span><span class="o">(</span><span class="s">&quot;AUGH&quot;</span><span class="o">);</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="s">&quot;fuh&quot;</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@GET</span>
    <span class="nd">@ResponseMetered</span>
    <span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/response-metered&quot;</span><span class="o">)</span>
    <span class="kd">public</span> <span class="n">Response</span> <span class="nf">responseMetered</span><span class="o">(</span><span class="nd">@QueryParam</span><span class="o">(</span><span class="s">&quot;invalid&quot;</span><span class="o">)</span> <span class="nd">@DefaultValue</span><span class="o">(</span><span class="s">&quot;false&quot;</span><span class="o">)</span> <span class="kt">boolean</span> <span class="n">invalid</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">invalid</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="n">Response</span><span class="o">.</span><span class="na">status</span><span class="o">(</span><span class="n">Response</span><span class="o">.</span><span class="na">Status</span><span class="o">.</span><span class="na">INTERNAL_SERVER_ERROR</span><span class="o">).</span><span class="na">build</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="n">Response</span><span class="o">.</span><span class="na">ok</span><span class="o">().</span><span class="na">build</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<div class="section" id="supported-annotations">
<h2>Supported Annotations</h2>
<p>Every resource method or the class itself can be annotated with &#64;Timed, &#64;Metered, &#64;ResponseMetered and &#64;ExceptionMetered.
If the annotation is placed on the class, it will apply to all its resource methods.</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">&#64;Timed</span></code> adds a timer and measures time spent in that method.</li>
<li><code class="docutils literal"><span class="pre">&#64;Metered</span></code> adds a meter and measures the rate at which the resource method is accessed.</li>
<li><code class="docutils literal"><span class="pre">&#64;ResponseMetered</span></code> adds a meter and measures rate for each class of response codes (1xx/2xx/3xx/4xx/5xx).</li>
<li><code class="docutils literal"><span class="pre">&#64;ExceptionMetered</span></code> adds a meter and measures how often the specified exception occurs when processing the resource.
If the <code class="docutils literal"><span class="pre">cause</span></code> is not specified, the default is <code class="docutils literal"><span class="pre">Exception.class</span></code>.</li>
</ul>
</div>
</div>


            </div></div>
        <hr/>
        <footer>
            <p style="float: left">
            &copy; Copyright 2010-2014, Coda Hale, Yammer Inc., 2014-2017 Dropwizard Team.
            Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>
            1.4a0+.
            </p>
            <p style="float: right">Dropwizard Metrics v4.1.2</p>
        </footer>
    </div>
</body>
</html>


